<?php
/**
 * $Id: Modules.php 121 2009-02-23 03:58:36Z hzq $
 *
 */
class Datas2 extends UModel
{
	protected $_name = 'datas2';
	
	public function getModules($type,$pid=1)
	{		
		
	}
	
	public function fetchIndex()
	{
		//http://www.591.com.tw/index.php?firstRow=0&totalRows=5033&module=house&action=rentbegBuy&type=3
		//http://sale.591.com.tw/index.php?firstRow=40&totalRows=4703&module=house&action=rentSale
		//http://sale.591.com.tw/sale-detail-40294.html
		//http://rent.591.com.tw/rent-detail-239890.html
	}
	
	public function fetchList($firstRow=0,$totalRows='5033',$datatype='3', $module='house',$action='rentbegBuy',$pageSize=40)
	{
		if($firstRow>800)return ;
	
			
		$url = 'http://www.591.com.tw/index.php?firstRow='.$firstRow.'&totalRows='.$totalRows.'&module='.$module.'&action='.$action.'&type='.$datatype;
		$data = file_get_contents($url);
		$this->saveData($data,$datatype);
		$firstRow += $pageSize;
		$this->fetchList($firstRow,$totalRows,$datatype);
		
	}
	
	public function fetchDetail($detailurl)
	{
			$url = 'http://www.591.com.tw/'.$detailurl;
			$data = @file_get_contents($url);
			if(empty($data))return false;
			return $this->saveDetail($data);
			
	}
	
	
	
	/**
	 * 保存591出租详细内容。
	 *
	 * @param string $data
	 * @param ing $rid
	 */
	public function saveDetail($data)
	{
		$aData = explode('<div class="detail">',$data);
		if(empty($aData[1]))return false;
		$aData = explode('<div class="Other_Box">',$aData[1]);
		
		if(empty($aData[0]))return false;
		
		$aData = explode('<div class="Remark" id="ubbRemark">',$aData[0]);
		
		$aNdata = explode('<td',$aData[0]);
		array_shift($aNdata);
		array_pop($aNdata);
		$aNAdata = array();
	
		foreach ($aNdata as $k=>$val){			
			if(($k+1)%2==0)$aNAdata[] = strip_tags('<td '.$val);
		}
		$memo = nl2br(strip_tags($aData[1]));
		$aNAdata[1] = str_replace(array('\r','\n'),'',$aNAdata[1]);
		$aNAdata[1] = trim($aNAdata[1]);
		$vvdata = explode('有效期限: ',$aNAdata[1]);
		
		$oneData = array(
			'a6' => $aNAdata[0] ,
			'a7' => $vvdata[0] ,
			'a8' => $vvdata[1] ,
			'a9' => $aNAdata[2] ,
			'a10' => $aNAdata[3] ,
			'a11' => $aNAdata[4] ,
			'a12' => $aNAdata[5] ,
			'a13' => $aNAdata[6] ,
			'a14' => $aNAdata[7] ,
			'a15' => $aNAdata[8] ,
			'a16' => $aNAdata[9] ,
			'a17' => $aNAdata[10] ,
			'a18' => $aNAdata[11] ,
			'a19' => $aNAdata[12] ,
			'a20' => $aNAdata[13] ,
			'a21' => $memo ,
		);
		
		return $oneData;
	}
	
	
	
	
	
	/**
	 * 保存591出租/出售列表
	 *
	 * @param string $data
	 * @param bool $isfirst
	 * @return int
	 */
	public function saveData($data,$datatype = '3')
	{
		//列表：
		if(empty($data))return false;
		$aData = explode('<div id="viewList">',$data);
		if(empty($aData[1]))return false;
		$data = $aData[1];
		$aData = explode('<!--viewList ends here -->',$data);
		if(empty($aData[0]))return false;
		$aData = explode('<div class="itemBottom">',$aData[0]);
		$a = $aData[0];
		$b = $aData[1];
		$aA = explode('<div class="inner-item">',$a);
		array_shift($aA);
		$aCitys = Caches::loadCache('city');
		$aArea = Caches::loadCache('area');
		$aCitys = array_flip($aCitys);
		$aArea = array_flip($aArea);
		if(is_array($aA))foreach ($aA as $one){
			preg_match('/<strong>([^<]+)<\/strong>/i',$one,$matcharea);
			preg_match('/<a href="([^"]+)" title="([^"]+)" target="_blank">[^<]+<\/a>/i',$one,$matchs);
			preg_match('/<span class="areaKind">([^<]+)<\/span>/i',$one,$matchsize);
			preg_match('/<span class="price">([^<]+)<\/span>/i',$one,$matchprice);
			preg_match('/<span class="browsenum">(\d+)<\/span>/i',$one,$matchhits);
			
			$aDetail = $this->fetchDetail($matchs[1]);
			if($aDetail){
				$oneData = array(
					'type' => $datatype,
					'a1' => $aCitys[$matcharea[1]],//地区
					'a2' => $matchs[2],//标题
					'a3' => $matchsize[1],//用途
					'a4' => $matchprice[1],//金额
					'a5' => $matchhits[1],//点击量
					
				);
				$oneData = array_merge($oneData,$aDetail);
				$this->createRow($oneData)->save();
			}
		}
		
	}
}